package net.huashitong.ssydt.provider.gexin.service;


import net.huashitong.ssydt.gexin.entity.SsPushGetui;
import net.huashitong.ssydt.provider.gexin.mapper.SsPushGetuiMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author <a href="mailto:yhy23456@163.com">huiyang.yu</a>
 * @since 2017.10.09
 */
@Service
@Transactional
public class SsPushGetuiServiceImpl implements SsPushGetuiService {

    private SsPushGetuiMapper mapper;

    @Autowired
    public SsPushGetuiServiceImpl(SsPushGetuiMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(SsPushGetui record) {
        return mapper.insert(record);
    }

    @Override
    public int insertSelective(SsPushGetui record) {
        return mapper.insertSelective(record);
    }

    @Override
    public SsPushGetui selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(SsPushGetui record) {
        return mapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(SsPushGetui record) {
        return mapper.updateByPrimaryKey(record);
    }

    @Override
    public SsPushGetui getByUserId(String userId) {
        return mapper.getByUserId(userId);
    }

    @Override
    public void register(String userId, String clientid) {
        SsPushGetui ssPushGetui = this.getByClientId(clientid);
        if (ssPushGetui != null) {
            if (ssPushGetui.getUserId().equals(userId)) {
                return;
            } else {
                ssPushGetui.setClientid(null);
                this.updateByPrimaryKey(ssPushGetui);
            }
        }
        ssPushGetui = this.getByUserId(userId);
        if (ssPushGetui == null) {
            ssPushGetui = new SsPushGetui();
            ssPushGetui.setUserId(userId);
            ssPushGetui.setClientid(clientid);
            this.insert(ssPushGetui);
        } else {
            ssPushGetui.setClientid(clientid);
            this.updateByPrimaryKey(ssPushGetui);
        }
    }

    @Override
    public SsPushGetui getByClientId(String clientid) {
        return mapper.getByClientId(clientid);
    }

    @Override
    public void deleteCancel(String userId) {
        SsPushGetui ssPushGetui = this.getByUserId(userId);
        if (ssPushGetui != null) {
            ssPushGetui.setClientid(null);
            this.updateByPrimaryKey(ssPushGetui);
        }
    }
}
